﻿using System;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace NDBGen.Mock.Reflexive.Tests
{
    /// <summary>
    /// Summary description for ReflexivePluginTests
    /// </summary>
    [TestClass]
    public class ReflexivePluginTests
    {
        [TestMethod]
        public void CompareSchema()
        {
            this.CompareFile("Schema", "dbgs");
        }

        [TestMethod]
        public void CompareLookupData()
        {
            this.CompareFile("LookupData", "dbgd");
        }

        [TestMethod]
        public void CompareSeedData()
        {
            this.CompareFile("SeedData", "dbgd");
        }

        public void CompareFile(String file, String extension)
        {
            var codeDir = new DirectoryInfo(Environment.CurrentDirectory).Parent.Parent.Parent;
            var sourcePath = Path.Combine(codeDir.FullName, String.Format(@"NDBGen.Mock.Reflexive.NDBGen\{0}.{1}", file, extension));
            var targetPath = Path.Combine(codeDir.FullName, String.Format(@"NDBGen.Mock.Reflexive\{0}.g.{1}", file, extension));

            var source = File.ReadAllLines(sourcePath).Where(l => !String.IsNullOrWhiteSpace(l) && !l.StartsWith("--")).ToArray();
            var target = File.ReadAllLines(targetPath).Where(l => !String.IsNullOrWhiteSpace(l) && !l.StartsWith("--")).ToArray();

            Assert.AreEqual(source.Count(), target.Count(), "Line count mismatch");

            for (var index = 0; index < source.Count(); index++)
            {
                var sourceLine = source[index];
                var targetLine = target[index];

                if (sourceLine == targetLine) continue;

                var sourceTabs = sourceLine.CountStartingTabs();
                var targetTabs = targetLine.CountStartingTabs();
                Assert.AreEqual(sourceTabs, targetTabs, "Tab mismatch on line {0}", index + 1);

                var sourceNoWhitespace = Regex.Replace(sourceLine, @"\s+", String.Empty);
                var targetNoWhitespace = Regex.Replace(targetLine, @"\s+", String.Empty);

                Assert.AreEqual(sourceNoWhitespace, targetNoWhitespace, "Markup mismatch on line {0}", index + 1);
            }
        }
    }
}